package com.imgdpicheck;

import com.imgdpicheck.utils.ImgCheckUtil;
import com.imgdpicheck.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

/**
 * @author xuehu
 * @date 2021/1/28 10:48
 */

public class ImageCheckDpi {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入全宗名称:");
        getImage(scanner.nextLine());

    }


    /**
     * 获取图片
     *
     * @param basGnlName 全宗名称
     * @return
     */
    public static void getImage(String basGnlName) {

        Connection connection = null;
        try {
            String sql = "select a.GNLARVNAM,b.* from ARV_BASINFO b, BAS_GNLARV a  where a.GNLARVID = b.GNLARVID and a.GNLARVNAM = " + "\'" + basGnlName + "\'";
            QueryRunner runner = new QueryRunner();
            MapListHandler handler = new MapListHandler();
            connection = JdbcUtils.getOracleConnection();
            List<Map<String, Object>> list = runner.query(connection, sql, handler);
            for (Map<String, Object> map : list) {
                List<Map<String, Object>> arvmap = getString(map.get("ARVID").toString());
                for (Map<String, Object> map1 : arvmap) {
                    if (!"0".equals(map1.get("PAGNUM").toString())) {
                        String sfzehName = map.get("OBJID").toString() + "--" + map.get("OBJNAM");
                        getFile(map1.get("FILEID").toString(), sfzehName, map1.get("FILETYPENAM").toString());
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (!connection.isClosed()) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }


    public static List<Map<String, Object>> getString(String arvId) {
        Connection connection = null;
        try {
            String sql = "select a.* from ARV_FIL_BASINFO a where a.ARVID = " + "\'" + arvId + "\'";
            QueryRunner runner = new QueryRunner();
            MapListHandler handler = new MapListHandler();
            connection = JdbcUtils.getOracleConnection();
            List<Map<String, Object>> list = runner.query(connection, sql, handler);
            return list;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (!connection.isClosed()) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void getFile(String fileID, String name, String typeName) {
        Connection connection = null;
        try {
            String sql = "SELECT * FROM ARV_FIL_PAGEINFO where FILEID = " + "\'" + fileID + "\'";
            QueryRunner runner = new QueryRunner();
            MapListHandler handler = new MapListHandler();
            connection = JdbcUtils.getOracleConnection();
            List<Map<String, Object>> list = runner.query(connection, sql, handler);
            File file = new File("D:" + File.separator + "dpi不合格图片");
            file.mkdir();
            list.forEach(s -> {
                File checkFile = new File("D:" + File.separator + "eamsdata" + s.get("ORIGIFILEPATH").toString());
                if (ImgCheckUtil.getImageWidthDpiByFile(checkFile) < 600 || ImgCheckUtil.getImageHeightDpiByFile(checkFile) < 600) {
                    File files = new File("D:" + File.separator + "dpi不合格图片" + File.separator + name);
                    files.mkdirs();
                    InputStream inputStream = null;
                    BufferedInputStream bufferedInputStream = null;
                    OutputStream outputStream = null;
                    BufferedOutputStream bufferedOutputStream = null;
                    try {
                        inputStream = new FileInputStream(checkFile);
                        bufferedInputStream = new BufferedInputStream(inputStream);
                        byte[] bytes = new byte[1024];
                        File filesss = new File("D:" + File.separator + "dpi不合格图片" + File.separator + name + File.separator + typeName);
                        filesss.mkdir();
                        File filess = new File("D:" + File.separator + "dpi不合格图片" + File.separator + name + File.separator + typeName + File.separator + s.get("SEQNO") + ".jpg");
                        outputStream = new FileOutputStream(filess);
                        bufferedOutputStream = new BufferedOutputStream(outputStream);
                        int len = 0;
                        while ((len = bufferedInputStream.read(bytes)) != -1) {
                            bufferedOutputStream.write(bytes, 0, len);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (bufferedInputStream != null) {
                            try {
                                bufferedInputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if (bufferedOutputStream != null) {
                            try {
                                bufferedOutputStream.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }


            });

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (!connection.isClosed()) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }


}
